/*
 * The purpose of this source is to be able to easely convert code that uses
 * standard file i/o to code that uses memory i/o. this is needed to convert
 * image and other structured data file importers to the ability to read from
 * memory, which is the case if the resource is read from zip file, for example.
 */

#ifndef INCLUDED_FM_H
#define INCLUDED_FM_H

#include <stdio.h>

/* fm_t type values */
#define FM_TYPE_FILE 1
#define FM_TYPE_MEM  2

struct fm_t
{
	int type;
	
	union {
		FILE* fp;
		struct {
			char *data, *curp;
			unsigned len;
		};
	};
};

/* the fm_open_xxx() functions are NZE, and caller must provide a struct fm_t object */
int fm_open_file(char* filename,struct fm_t* fm); /* always opens in binary mode */
int fm_open_mem(char* data, unsigned len,struct fm_t* fm);

/* these functions mimic the fxxx() functions from stdio.h closely in semantics and error behaviour */
int fm_close(struct fm_t* fm);
int fm_seek(struct fm_t* fm, long offset, int whence);
size_t fm_read(void* ptr, size_t size, size_t nmemb,struct fm_t* fm);
void fm_rewind(struct fm_t* fm);

#endif /* INCLUDED_FM_H */
